package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.service.MasterRPCService;
import id.net.iconpln.bb.client.service.MasterRPCServiceAsync;
import id.net.iconpln.bb.client.service.TransportRPCService;
import id.net.iconpln.bb.client.service.TransportRPCServiceAsync;
import id.net.iconpln.bb.client.util.ClientModule;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.core.FastMap;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class PageBbTransportJadwal extends LayoutContainer {
	private final MasterRPCServiceAsync serviceMaster = GWT.create(MasterRPCService.class);
	private final TransportRPCServiceAsync service = GWT.create(TransportRPCService.class);
	private Grid gridPembangkit, gridJadwal;
	private ListStore storePembangkit, storeJadwal;
	
	  @Override  
	  protected void onRender(Element parent, int index) {  
	    super.onRender(parent, index);  
	    setLayout(new FitLayout());
	    
	    ContentPanel cp = new ContentPanel();  	   
	    cp.setHeaderVisible(false);
	    cp.setFrame(true);  
	    cp.setLayout(new RowLayout(Orientation.HORIZONTAL)); 
	    
	    createGridPembangkit();
	    gridPembangkit.getSelectionModel().addListener(Events.SelectionChange,  
    		new Listener<SelectionChangedEvent>() {  
    		public void handleEvent(SelectionChangedEvent be) {  
    			Map mapParam = new FastMap();
    			mapParam.put("id_pemasok", "30");
    			mapParam.put("id_pltu", be.getSelectedItem().get("id_pltu"));
			  
    			service.getListPengendalian("JADWAL_BY_PLTU_PEMASOK", mapParam,new AsyncCallback<List<Map>>() {
    				public void onFailure(Throwable caught) {
    					GWT.log(caught.getMessage(), null);
    				}

    				public void onSuccess(List<Map> result) {	
    					storeJadwal.removeAll();
    					storeJadwal.add(ClientModule.setListBaseModelFromListMap(result));
    				}
    			});
			}  
        });
	    
	    ToolBar tbPembangkit = new ToolBar();
	    tbPembangkit.add(new LabelToolItem("<b>List Pembangkit</b>"));
	    tbPembangkit.add(new SeparatorToolItem());
	    
	    ContentPanel pg2 = new ContentPanel();
	    pg2.setHeaderVisible(false);
	    pg2.setLayout(new RowLayout(Orientation.HORIZONTAL)); 
	    pg2.add(gridPembangkit, new RowData(1, 1));
	    pg2.setTopComponent(tbPembangkit);
	    cp.add(pg2, new RowData(.4, 1));
	    
	    ContentPanel pg1 = new ContentPanel();
	    pg1.setLayout(new RowLayout(Orientation.HORIZONTAL)); 
	    pg1.setHeaderVisible(false);
	  
	    createGridJadwal();  
	    gridJadwal.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);  
	    
	    ToolBar tbJadwal = new ToolBar();
	    tbJadwal.add(new LabelToolItem("<b>Jadwal Pengiriman</b>"));
	    tbJadwal.add(new SeparatorToolItem());	  
	    pg1.add(gridJadwal,new RowData(1, 1));
	    pg1.setTopComponent(tbJadwal);
	    
	    ContentPanel delimeter = new ContentPanel();
	    delimeter.setHeaderVisible(false);
	    delimeter.setWidth(10);
	    cp.add(delimeter, new FitData(5));
	    cp.add(pg1, new RowData(.6, 1));
	    add(cp);  
	  }  
	  
	  private void createGridPembangkit(){
		    List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  
		    ColumnConfig column = new ColumnConfig();	
		    
		    RowNumberer id = new RowNumberer();  
		    id.setWidth(30);
		    configs.add(id);
		    
		    column = new ColumnConfig("nama_pembangkit", 250);  
		    column.setHeader("Pembangkit");    
		    configs.add(column);  
		    
		    storePembangkit = new ListStore();  
		    storePembangkit.setMonitorChanges(true);  
		    serviceMaster.getMaster("KIT", new FastMap(), new AsyncCallback<List<Map>>() {
				
				@Override
				public void onSuccess(List<Map> result) {
					// TODO Auto-generated method stub
					storePembangkit.removeAll();
					storePembangkit.add(ClientModule.setListBaseModelFromListMap(result));
				}
				
				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					GWT.log(caught.getMessage());
				}
			});
		    
		    ColumnModel cm = new ColumnModel(configs);  		  
		    gridPembangkit = new Grid(storePembangkit, cm);  
		    gridPembangkit.getView().setEmptyText("Data Empty");  
		    gridPembangkit.setBorders(true);  
	  }   
	  
	  private void createGridJadwal() {  
	    List<ColumnConfig> configs = new ArrayList<ColumnConfig>();  

	    ColumnConfig column = new ColumnConfig();	
	    column = new ColumnConfig("no_jadwal", "No Jadwal", 150);  
	    configs.add(column);  

	    column = new ColumnConfig("bulan_tahun", "BLTH", 100);  
	    configs.add(column);  	  
	    		  
	    column = new ColumnConfig("tanggal", "Tanggal", 100);  
	    configs.add(column);
	    
	    column = new ColumnConfig("nama_transportir", "Transportir", 150);  
	    configs.add(column);
	    
	    column = new ColumnConfig("nama_transport", "Kapal", 150);  
	    configs.add(column);
	    
	    column = new ColumnConfig("volume", "Vol (Ton)", 150);  
	    configs.add(column);
	    
	    column = new ColumnConfig("tgl_td", "ETD", 150);  
	    configs.add(column);
	    
	    column = new ColumnConfig("tgl_ta", "ETA", 150);  
	    configs.add(column);
	    
	    column = new ColumnConfig("status", "Status", 150);  
	    configs.add(column);
	    
	    column = new ColumnConfig("keterangan", "Keterangan", 150);  
	    configs.add(column);
	    
	    storeJadwal = new ListStore();  
	    storeJadwal.setMonitorChanges(true);  
	  
	    ColumnModel cm = new ColumnModel(configs);  
	  
	    gridJadwal = new Grid(storeJadwal, cm);  
	    gridJadwal.getView().setEmptyText("Data Empty");  
	    gridJadwal.setBorders(true);  
	  }  
}
